
const camelizeRE = /-(\w)/g
interface phoneNumberFilterParams {
  value:string,
  type?:number, // 0 只允许数字 1 允许数字以及-
}

export function camelize (str: string): string {
  return str.replace(camelizeRE, (_, c) => c.toUpperCase())
}

export function padZero (num: number | string, targetLength = 2): string {
  let str = num + ''
  
  while (str.length < targetLength) {
    str = '0' + str
  }
  
  return str
}

/**
 * @Descripttion 去除富文本内容中的标签等
 * @param {*} str 富文本内容
 * @returns 去除标签后的内容
 */
 export function removeLabel(str:string) :string {
  return str.replace(/<(p|div)[^>]*>(<br\/?>|&nbsp;)<\/\1>/gi, '\n').replace(/<br\/?>/gi, '\n').replace(/<[^>/]+>/g, '')
    .replace(/(\n)?<\/([^>]+)>/g, '')
    .replace(/\u00a0/g, ' ')
    .replace(/&nbsp;/g, ' ')
    .replace(/<\/?(img)[^>]*>/gi, '')
    .replace(/&amp;/g, '&')
    .replace(/&lt;/g, '<')
    .replace(/&gt;/g, '>')
    .replace(/&#39;/g, '\'')
    .replace(/&quot;/g, '"')
    .replace(/<\/?.+?>/g, '')
}

/**
 * @des 用作所有电话类的文本过滤
 * @param str 
 * @returns 
 */
export function phoneNumberFilter({value, type = 1}:phoneNumberFilterParams) :string{
  const RE = type === 1 ? /[^\d/-]/g : /[^\d]/g;
  try {
    // 86区号自动过滤
    const AreaRE = /^86/;
    const AreaRE_3 = /(^\+86)|(^086)/;
    const AreaRE_4 = /^0086/

    value = value.replace(RE, '');
    // 如果是11位手机号格式自动过滤掉头部的+86、086、86 不是11位手机号则不做处理
    if(AreaRE_3.test(value) && value.length === 14){
      value = value.replace(AreaRE_3, '')
    }else if(AreaRE.test(value) && value.length === 13){
      value = value.replace(AreaRE, '')
    }else if(AreaRE_4.test(value) && value.length === 15){
      value = value.replace(AreaRE_4, '')
    }
    return value;
  } catch (error) {
    console.warn(error, 'error try catch phoneNumberFilter');
    return value
  }
}
